// pages/index/index.js
let shopApi = require('../../api/shop.js')
let userApi = require('../../api/user.js')
import Toast from '@vant/weapp/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeTab: 0,
    shopType: 0,
    tabs: [{
        title: '运动场馆',
        shopType: 0
      },
      {
        title: 'KTV',
        shopType: 1
      },
      {
        title: '饭店',
        shopType: 2
      }
    ],
    page: {
      pageNum: 1,
      pageSize: 10
    },
    dataList: []
  },

  doLogin() {
    if (getApp().globalData.token) {
      return;
    }
    wx.getUserInfo({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        let userInfo = {};
        userInfo.password = res.iv;
        userInfo.username = res.userInfo.nickName;
        userInfo.userAvatar = res.userInfo.avatarUrl;
        userInfo.userType = 1;
        userApi.doLogin(userInfo)
          .then(res => {
            getApp().globalData.token = res.data.data.token;
            getApp().globalData.userInfo = res.data.data.user;
          })
          .catch(err => {
            console.log('error')
          })
      }
    })
  },

  fetchShopData() {
    var that = this;
    shopApi.queryShop(that.data.page, that.data.shopType)
      .then(res => {
        console.log("res", res)
        res = res.data;
        if (res.code === 200) {
          that.setData({
            dataList: res.data.list
          });
          if(res.data.list.size===0 && that.data.page.pageNum===1){
              Toast.success('无数据')
          }
        } else {
          Toast.fail("请求失败")
        }
      }).catch(error => {
        Toast.fail("请求失败")
        console.log(error)
      })
  },

  goToDetail(e){
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/shopDetail/shopDetail?id='+id,
    })
  },

  loadMore(){
    this.data.page.pageNum++;
    let that = this;
    shopApi.queryShop(this.data.page, this.data.shopType)
    .then(res=>{
        res = res.data;
        console.log(res,'res.......')
        if(res.code === 200){
            let data = res.data.list
            if(data && data.size >0){
              let list = that.data.dataList.concat(data)
              that.data.dataList = list;
            }else{
              that.data.page.pageNum--;
              Toast.success("无更多数据")
            }
        }else{
          Toast.fail("请求失败")
        }
    })
    .catch(error => {
      Toast.fail("请求失败")
      console.log(error)
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.doLogin();
    this.fetchShopData();
  },

  onClickTab(e) {
    console.log(e.detail)
    const index = e.detail.index
    this.setData({
      activeTab: index
    })
    this.setData({
      shopType: this.data.tabs[index].shopType
    })
    console.log('change')
    this.fetchShopData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
      this.data.page.pageNum = 1;
      this.fetchShopData();
      Toast.success('刷新成功')
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})